<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.faf.bean.Food"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort() + path+"/"+"UserGlobal"+"/";
%>
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>饭否-购物车</title>
	<link rel="stylesheet" type="text/css" href="<%=basePath%>css/reset.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath%>css/main.css">
	<script type="text/javascript" src="<%=basePath%>js/jquery-1.12.4.min.js"></script>
	<script type="text/javascript" src="<%=basePath%>js/jquery-ui.min.js"></script>
	<script type="text/javascript" src="<%=basePath%>js/slide.js"></script>
</head>
<% 
	Object pre_global_info_user_name = request.getSession().getAttribute("account");
	String global_info_user_name;
	HashMap<Integer, Integer> global_info_cart_id2count = null;
	int i_food_count = 0;
	boolean isUserInfoOver = false;
	
	if(null != pre_global_info_user_name){
		boolean a = null != session.getAttribute("address");
		boolean b = null != session.getAttribute("name");
		boolean c = null != session.getAttribute("phone");
		isUserInfoOver = a&&b&&c;
		global_info_user_name = pre_global_info_user_name.toString();
		global_info_cart_id2count = (HashMap<Integer, Integer>)request.getSession().getAttribute("global_cart");
		for(int value : global_info_cart_id2count.values())
			i_food_count += value;
		
	} else {
		global_info_user_name = "";
	}

	request.getSession().setAttribute("pre-page", "/FAFCode/MyCart" + "?" + request.getQueryString());
	
	HashMap<Integer, ArrayList<Integer>> hash_store2food = (HashMap<Integer, ArrayList<Integer>>)request.getAttribute("hash_store2food");
	HashMap<Integer, ArrayList<Integer>> hash_store2foodcount = (HashMap<Integer, ArrayList<Integer>>)request.getAttribute("hash_store2foodcount");
	HashMap<Integer, Food> hash_fid2food = (HashMap<Integer, Food>)request.getAttribute("hash_fid2food");
%>
<% 
	if(null == pre_global_info_user_name) {
%>
	<jsp:forward page="/FAFCode/UserManagement/login.jsp"/>
<%
	} else { 
%>
<body>
	<div class="header_con">
		<div class="header">
			<div class="welcome fl">欢迎来到饭否!</div>
			<div class="fr">
				<div class="login_info fl">
					欢迎您：<em><%= null == pre_global_info_user_name ? global_info_user_name : "请登录" %></em>
				</div>
				<% if(null == pre_global_info_user_name) { %>
				<div class="login_btn fl">
					<a href="/FAFCode/UserManagement/login.jsp">登录</a>
					<span>|</span>
					<a href="/FAFCode/UserManagement/register.jsp">注册</a>
				</div>
				<% } else { %>
				<div class="user_link fl">
					<a href="/FAFCode/UserManagement/user_center_info.jsp">用户中心</a>
					<span>|</span>
					<a href="/FAFCode/MyCart">我的购物车</a>
					<span>|</span>
					<a href="/FAFCode/UserManagement/user_center_order.jsp">我的订单</a>
				</div>
				<% } %>
			</div>
		</div>		
	</div>
	
	<div class="search_bar clearfix">
		<a href="Index" class="logo fl"><img src="/FAFCode/Image/logo/logo.png"></a>
		<form action="List" >
		<div class="search_con fl">
			<input type="text" class="input_text fl" name="serchContect" placeholder="搜索商品">
			<input type="submit" class="input_btn fr" name="" value="搜索">
		</div>
		</form>
		<div class="guest_cart fr">
			<a href="/FAFCode/MyCart" class="cart_name fl">我的购物车</a>
			<div class="goods_count fl" id="show_count"><%=i_food_count %></div>
		</div>
	</div>
	
	<%
		for(HashMap.Entry<Integer, ArrayList<Integer>> entry : hash_store2food.entrySet()){
			ArrayList<Integer> foodidList = entry.getValue();
			ArrayList<Integer> foodnumList = hash_store2foodcount.get(entry.getKey());
			int icount = 0;
			double iprice = 0;
			int storeId = entry.getKey();
	%>
	<div class="total_count">全部商品<em><%=foodidList.size() %></em>件</div>	
	<ul class="cart_list_th clearfix">
		<li class="col01">商品名称</li>
		<li class="col02">商品单位</li>
		<li class="col03">商品价格</li>
		<li class="col04">数量</li>
		<li class="col05">小计</li>
		<li class="col06">操作</li>
	</ul>
	<%
		for(int i = 0;i < foodidList.size();i ++){
			Food food = hash_fid2food.get(foodidList.get(i));
			icount += foodnumList.get(i);
			iprice += food.getFoodPrice()*foodnumList.get(i);
	%>
	<ul class="cart_list_td clearfix">
		<li class="col01"><input type="checkbox" name="" checked></li>
		<li class="col02"><img src="<%= "/FAFCode/"+food.getFoodPhoto() %>"></li>
		<li class="col03"><%=food.getFoodName() %><br><em><%=food.getFoodPrice() %>元/1份</em></li>
		<li class="col04">1份</li>
		<li class="col05" name=<%="sp_"+storeId %>><%=food.getFoodPrice() %>元</li>
		<li class="col06">
			<div class="num_add">
				<a href="#" onclick=<%="editfoodcount("+food.getFoodId()+",1)" %> class="add fl">+</a>
				<input type="text" id=<%=food.getFoodId()+"_num" %> class="num_show fl" value="<%=foodnumList.get(i) %>">	
				<a href="#" onclick=<%="editfoodcount("+food.getFoodId()+",0)" %> class="minus fl">-</a>	
			</div>
		</li>
		<li class="col07"><%=food.getFoodPrice()*foodnumList.get(i) %>元</li>
		<li class="col08"><a href="<%="DropCart?fid="+food.getFoodId() %>">删除</a></li>
	</ul>
	<%
		}
	%>
	<ul class="settlements">
		<li class="col01"><input type="checkbox" name="" checked=""></li>
		<li class="col02">全选</li>
		<li class="col03">合计(不含运费)：<span>¥</span><em><%=iprice %></em><br>共计<b><%=icount %></b>件商品</li>
		<li class="col04"><a href="<%= "PlaceOrder?storeid=" + entry.getKey() %>">去结算</a></li>
	</ul>
	<%
		}}
	%>
</body>
<script type="text/javascript">
	function editfoodcount(fid, action){
        $.ajax({
            method: "post",
            data: {
                "fid": fid,
                "action": action,
            },
            dataType: "json",
            url: "/FAFCode/EditCart",
            success: function (answer) {
            	console.log(answer.res);
            	if(answer.res == "Succ"){
            		var obj_name = fid+"_num";
            		var obj = document.getElementById(fid+"_num");
            		if(action == 1){
            			obj.value ++;
            		}
            		else{
            			obj.value = obj.value == 0 ? 0 : obj.value - 1;
            		}
            		window.location.href = "/FAFCode/MyCart"
            	}
            },
            error: function () {
                alert("ERROR 404")
            }
        })
	}
</script>
</html>